
#include<iostream>
using namespace std;
#include<cmath>
#include<fstream>

#include"library.h"

/* Euler(...)		solve 1-order ODE for initial problem
 * double a		[a,b]
 * double b
 * double *func(double x, double y)	f(x,y)=dy/dx
 * int n		[a,b] is divided into n pieces
 * double y0		initial value y(a)
 * double *y		y[n+1] stores y0, y1,..., yn
 */
void Euler(double a, double b, double (*func)(double x, double y), int n, double y0, double *y){
	
	int i;
	double h = (b-a)/n; //步长
	y[0] = y0;
	for(i=0;i<n;i++){
		y[i+1] = y[i] + h*func(a + i*h, y[i]);
	}
	ofstream fp("Euler.txt");
	for(i=0;i<=n;i++){
		fp<<a+i*h<<"\t"<<y[i]<<endl;
	}
	fp.close();
}

double f(double x, double y){
	return 10 - y*y;
}

int main(){

	int i,j,k;

	int n=10000;
	double *y = new double [n+1];
	Euler(0, 2, f, n, 0, y);

	delete [] y;
	return 0;
}
